package com.why.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.why.entity.Courseselection;
import com.why.entity.Orders;
import com.why.entity.Students;
import com.why.entity.Teachers;
import com.why.service.CourseselectionService;
import com.why.service.CoursetypeService;
import com.why.service.OrdersService;
import com.why.service.StudentsService;
import org.apache.coyote.Response;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 学生(Students)表控制层
 *
 * @author makejava
 * @since 2023-05-01 17:23:15
 */
@RestController
@CrossOrigin
@RequestMapping("/student")
public class StudentsController {
    /**
     * 服务对象
     */
    @Autowired
    private StudentsService studentsService;
    @Autowired
    private CourseselectionService courseselectionService;
    @Autowired
    private OrdersService ordersService;

    /**
     * 学生登录
     *
     * @param request
     * @param student
     * @return
     */
    @PostMapping("/login")
    public String Login(HttpServletRequest request, @RequestBody Students student) {
        LambdaQueryWrapper<Students> queryWrapper = new LambdaQueryWrapper<Students>();
        queryWrapper.eq(Students::getXueshengzhanghao, student.getXueshengzhanghao()).eq(Students::getMima, student.getMima());
        Students user = studentsService.getOne(queryWrapper);
        System.out.println(user);
        if (user == null) {
            return "error";
        } else {
            request.getSession().setAttribute("StudentId", user.getId());
            return "success";
        }
    }

    @PostMapping("/logout")
    public String Logout(HttpServletRequest request) {
        request.getSession().removeAttribute("StudentId");
        return "success";
    }


    /**
     * 学生注册
     *
     * @param student
     * @return
     */
    @PostMapping("/save")
    public String save(@RequestBody Students student) {
        studentsService.save(student);
        return "success";
    }

    /**
     * 分页查询学生信息
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Page<Students> page(int page, int pageSize, String name) {
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Students> queryWrapper = new LambdaQueryWrapper<Students>();
        queryWrapper.like(name != null, Students::getXueshengxingming, name);
        queryWrapper.orderByDesc(Students::getId);
        studentsService.page(pageInfo, queryWrapper);
        return pageInfo;
    }

    /**
     * 获取对应学生的选课信息
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/select/page")
    public Page<Courseselection> page1(int page, int pageSize, String name, HttpServletRequest request) {
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Courseselection> queryWrapper = new LambdaQueryWrapper<Courseselection>();
        Long id = (Long) request.getSession().getAttribute("StudentId");
        queryWrapper.like(Courseselection::getCoursename, name);
        //根据学生ID查找
        queryWrapper.eq(Courseselection::getStudentid, id);
        queryWrapper.orderByDesc(Courseselection::getAddtime);
        courseselectionService.page(pageInfo, queryWrapper);
        return pageInfo;
    }

    /**
     * 根据学生Id分页查询对应的订单信息
     *
     * @param page
     * @param pageSize
     * @param name
     * @param request
     * @return
     */
    @GetMapping("/order/page")
    public Page<Orders> page2(int page, int pageSize, String name, HttpServletRequest request) {
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<Orders>();
        Long id = (Long) request.getSession().getAttribute("StudentId");
        queryWrapper.like(Orders::getCoursename, name);
        //根据学生ID查找
        queryWrapper.eq(Orders::getStudentid, id);
        queryWrapper.orderByDesc(Orders::getAddtime);
        ordersService.page(pageInfo, queryWrapper);
        return pageInfo;
    }


    /**
     * 通过session获取信息
     *
     * @param request
     * @return
     */
    @GetMapping("/getstudent")
    public Students getAdmin(HttpServletRequest request) {
        Long id = (Long) request.getSession().getAttribute("StudentId");
        LambdaQueryWrapper<Students> queryWrapper = new LambdaQueryWrapper<Students>();
        queryWrapper.eq(Students::getId, id);
        Students Student = studentsService.getOne(queryWrapper);
        return Student;
    }


    @PostMapping("/update")
    public String update(@RequestBody Students student, HttpServletRequest request) {
        Long Id = student.getId();
        LambdaQueryWrapper<Students> queryWrapper = new LambdaQueryWrapper<Students>();
        queryWrapper.eq(Students::getId, Id);
        studentsService.update(student, queryWrapper);
        return "success";
    }

    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id) {
        studentsService.removeById(id);
        return "success";
    }
}

